import axios from "axios"
import ElementUI from 'element-ui';
//导入页面加载虚拟进度条
import NProgress from 'nprogress'
export  const request =(options)=> {
    return new Promise((resolve, reject) => {
        const service = axios.create()
        if(process.env.NODE_ENV=='development'){
            service.defaults.baseURL = '/api'
        }else{
            service.defaults.baseURL = 'https://grove.cpzsyun.com:82/ryapi/foster'
        }
        service.timeout= 20000 ;
        service.interceptors.request.use(
            (config) => {
                NProgress.start()
                let token = sessionStorage.getItem("token")
                if (token) {
                    config.headers['token'] = token
                }
                return config
            }
        )
        service.interceptors.response.use(
            (response) => {
                NProgress.done()
                if(response.data.code!=200&&response.data.type!='application/octet-stream'){
                   ElementUI.Message.error(response.data.msg)
                }
                return response.data
            }, (error) => {
                NProgress.done()
                if(error.request.responseType == 'blob'){
                    ElementUI.Message.error('导出失败')
                    return Promise.reject(error)
                }else{
                    ElementUI.Message.error('服务器错误')
                    return Promise.reject(error)
                }
            }
        )
        // 请求处理
        service(options)
            .then((res) => {
                resolve(res)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export default request